<template>
  <div>
    <div class="dashboard-container">
      <div class="app-container">
        <h2>设置</h2>
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="角色管理" name="first">
            <el-row style="height: 60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog = true">新增角色</el-button>
              <!-- 表格 -->

              <!-- 分页组件 -->
            </el-row>
            <el-row>
              <el-table :data="list" borde>
                <el-table-column label="序号" type="index" width="120" />
                <el-table-column label="角色名称" prop="name" width="240" />
                <el-table-column label="描述" prop="description" />
                <el-table-column label="操作">
                  <template slot-scope="{ row }">
                    <el-button size="small" type="success" @click="assignPerm(row.id)">分配权限</el-button>
                    <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                    <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-row>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination :current-page="page.page" :page-size="page.pagesize" :total="page.total" layout="prev, pager, next" @current-change="changePage"> </el-pagination>
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息" name="second">
            <el-alert title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改" type="info" show-icon :closable="true" />
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input v-model="formData.name" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width: 400px" />
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width: 400px" />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>

        <el-dialog :title="showTitle" :visible="showDialog" @close="btnCancel">
          <el-form ref="roleForm" :model="roleForm" label-width="120px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>
          <!-- 底部 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button size="small" @click="btnCancel">取消</el-button>
              <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            </el-col>
          </el-row>
        </el-dialog>

        <el-dialog title="分配权限" :visible="showPermDialog" @close="btnPermCancel">
          <!-- 权限是一颗树 -->
          <!-- 将数据绑定到组件上 -->
          <!-- check-strictly 如果为true 那表示父子勾选时  不互相关联 如果为false就互相关联 -->
          <!-- id作为唯一标识 -->
          <el-tree ref="permTree" :data="permData" :props="defaultProps" :show-checkbox="true" :check-strictly="true" :default-expand-all="true" :default-checked-keys="selectCheck" node-key="id" />
          <!-- 确定 取消 -->
          <el-row slot="footer" type="flex" justify="center">
            <el-col :span="6">
              <el-button type="primary" size="small" @click="btnPermOK">确定</el-button>
              <el-button size="small" @click="btnPermCancel">取消</el-button>
            </el-col>
          </el-row>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole, getRoleDetail, updateRole, addRole, assignPerm } from '@/api/setting'
import { mapGetters } from 'vuex'
import { tranListToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permission'
export default {
  name: 'Setting',

  data() {
    return {
      activeName: 'first',
      page: {
        page: 1,
        pagesize: 5,
        total: 0
      },
      formData: {
        name: '',
        companyAddress: '',
        mailbox: '',
        remarks: ''
      },
      list: [],
      showDialog: false,
      roleForm: {},
      showPermDialog: false, // 控制分配权限弹层的显示后者隐藏
      defaultProps: {
        label: 'name'
      },
      permData: [], // 专门用来接收权限数据 树形数据
      selectCheck: [], // 定义一个数组来接收 已经选中的节点
      roleId: null // 用来记录分配角色的id
    }
  },

  created() {
    this.getRoleList()

    setTimeout(() => {
      this.getCompanyInfo()
    }, 500)
  },

  methods: {
    handleClick(tab, event) {
      console.log(tab, event)
    },
    async getRoleList() {
      const res = await getRoleList(this.page)
      this.page.total = res.data.data.total
      this.list = res.data.data.rows
    },
    changePage(currentPage) {
      this.page.page = currentPage
      this.getRoleList()
    },
    async getCompanyInfo() {
      const res = await getCompanyInfo(this.companyId)
      this.formData = res.data.data
    },
    async deleteRole(id) {
      console.log('id:', id)
      try {
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log('error:', error)
      }
    },
    async editRole(id) {
      const res = await getRoleDetail(id)
      this.roleForm = res.data.data
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
      this.roleForm = {}
    },
    async btnOK() {
      console.log('role.Form:', this.roleForm.id)
      if (this.roleForm.id) {
        console.log('编辑角色')
        await updateRole(this.roleForm)
      } else {
        console.log('新增角色')
        await addRole(this.roleForm)
      }
      this.getRoleList()
      this.$message.success('操作成功')
      this.showDialog = false
      this.roleForm = {}
    },
    btnPermCancel() {
      this.selectCheck = [] // 重置数据
      this.showPermDialog = false
    },
    async assignPerm(id) {
      const res = await getPermissionList()
      this.permData = tranListToTreeData(res.data.data, '0')
      this.roleId = id
      const res1 = await getRoleDetail(id) // permIds是当前角色所拥有的权限点数据
      this.selectCheck = res1.data.data.permIds // 将当前角色所拥有的权限id赋值
      this.showPermDialog = true
    },
    async btnPermOK() {
      await assignPerm({ permIds: this.$refs.permTree.getCheckedKeys(), id: this.roleId })
      this.$message.success('分配权限成功')
      this.showPermDialog = false
    }
  },
  computed: {
    ...mapGetters(['companyId']),
    showTitle() {
      return this.roleForm.id ? '编辑角色' : '新增角色'
    }
  }
}
</script>

<style lang="scss" scoped></style>
